package com.errands.cn.controller;

import com.errands.cn.entity.OrderReceiving;
import com.errands.cn.entity.Orders;
import com.errands.cn.entity.User;
import com.errands.cn.services.OrderRecivingService;
import com.errands.cn.services.OrderService;
import com.errands.cn.utils.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Null;
import java.util.Date;
import java.util.List;

@RestController
public class RecivingController {
    @Autowired
    private OrderRecivingService recivingService;
    @Autowired
    private OrderService orderService;

    @PostMapping("/reciving_order")
    public Json RecivingOrder(@RequestParam("oId") String oId, HttpServletRequest request){
        Json json = new Json();
       User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            json.setMsg("你还没登录，无法接单,请先登录");
            return json;
        }
        Orders order = orderService.findByOrderId(oId);
        System.out.println(order.getoState());
        if (order.getoState() != 0){
            json.setMsg("该订单已经被接单");
            return json;
        }
        OrderReceiving receiving = new OrderReceiving();
        receiving.setoId(order.getoId());
        receiving.setUid(user.getUid());
        receiving.setSendUid(order.getUid());
        receiving.setReciveDate(new Date());
        receiving.setState(1);
        if(recivingService.save(receiving)){
            Orders orders = new Orders();
            orders.setoId(order.getoId());
            orders.setoState(1);
            orderService.updateOrder(orders);
            json.setSuccess(true);
        }else {
            json.setMsg("接单失败！");
        }
        return json;
    }

    @GetMapping("/recived/delete")
    public boolean deleteReciving(@RequestParam("rId") Integer rId){
        return  recivingService.deleteRecivedOrder(rId);
    }

    @GetMapping("/recived/give_up")
    public boolean give_up(@RequestParam("rId") Integer rId){
        OrderReceiving receiving = recivingService.getOrderReceingById(rId);
        OrderReceiving orderReceiving = new OrderReceiving();
        orderReceiving.setId(rId);
        orderReceiving.setState(4);
        Orders orders = new Orders();
        orders.setoId(receiving.getoId());
        orders.setoState(0);
        return recivingService.update(orderReceiving);
    }

    @GetMapping("/recived/get")
    public List<OrderReceiving> getOrderReceving(@RequestParam("state") Integer state,HttpServletRequest request){
        if (state == -1){
            state = null;
        }
        //User user = (User) request.getSession().getAttribute("user");
        return recivingService.getOrderReceving(state,12);
    }

}
